<?php

  # Author: Tomasz Durka
  # Created at: 5th April 2007
  
  function date_select ($params = array()) {
    
    # Defaults:
    $name = '';
    $date = new Date();
    
    
    $show_date = true;
    $show_years = true;
    $show_months = true;
    $show_days = true;
    
    
    $show_time = false;
    $show_hours = true;
    $show_minutes = true;
    $show_seconds = true;
    
    $minutes_interval = 1;
    
    foreach ($params as $_name => $_value) {
      switch ($_name) {
        case 'name':
        case 'show_date':
        case 'show_years':
        case 'show_months':
        case 'show_days':
        case 'show_time':
        case 'show_hours':
        case 'show_minutes':
        case 'show_seconds':
        case 'minutes_selected':
        case 'minutes_interval':
          $$_name = $_value;
          break;
        case 'date':
          $$_name = new Date($_value);
          break;
      }
    }
    
    
    
    
    # Grouped name check
    if ($name != "") {
      $name_prefix = "{$name}[";
      $name_suffix = "]";
    }
    
    
    
    
    # Selects assign
    $returned = '';
    
    if ($show_date) {
    
    
      if ($show_days) {
    
        # Days
        $days = array();
        for ($i = 1; $i <= 31; $i++) {
          $days[$i] = $i;
        }
        $returned .= select (array(
                        'name' => $name_prefix . 'day' . $name_suffix, 
                        'options' => $days, 
                        'selected' => $date->date_format("%d")));
    
      }
      if ($show_months) {    
        # Months
        $months = array();
        for ($i = 1; $i <= 12; $i++) {
          $months[$i] = strftime("%B", mktime(0, 0, 0, $i, 1, 2000));
        }
        $returned .= select (array(
                        'name' => $name_prefix . 'month' . $name_suffix, 
                        'options' => $months, 
                        'selected' => $date->date_format("%m")));
        
      }
      if ($show_years) {
        # Years
        $current_year = date("Y");
        for ($i = ($current_year - 2); $i <= $current_year; $i++) {
          $years[$i] = $i;
        }
        $returned .= select (array(
                        'name' => $name_prefix . 'year' . $name_suffix, 
                        'options' => $years, 
                        'selected' => $date->date_format("%Y")));
      }
    }
    
    
    
    
    
    if ($show_time) {
    
    
      if ($show_hours) {
    
        # Days
        $hours = array();
        for ($i = 0; $i <= 24; $i++) {
          $hours[$i] = str_pad($i, 2, "0", STR_PAD_LEFT);
        }
        $returned .= select (array(
                        'name' => $name_prefix . 'hour' . $name_suffix, 
                        'options' => $hours, 
                        'selected' => $date->date_format("%H")));
    
      }
      if ($show_minutes) {    
        # Months
        $minutes = array();
        for ($i = 0; $i < 60; $i++) {
          if ($i % $minutes_interval == 0) {
            $minutes[$i] = str_pad($i, 2, "0", STR_PAD_LEFT);
          }
        }
        $minutes_selected = ($minutes_selected !== false) ? $minutes_selected : $date->date_format("%M");
        
        $returned .= select (array(
                        'name' => $name_prefix . 'minute' . $name_suffix, 
                        'options' => $minutes, 
                        'selected' => $minutes_selected));
        
      }
      if ($show_seconds) {
        # Years
        for ($i = 0; $i <= 60; $i++) {
          $j = str_pad($i, 2, "0", STR_PAD_LEFT);
          $seconds[$j] = $j;
        }
        $returned .= select (array(
                        'name' => $name_prefix . 'second' . $name_suffix, 
                        'options' => $seconds, 
                        'selected' => $date->date_format("%S")));
      }
    }
      
    
    return $returned;
    
  }
  
?>